package com.pixiv.servlet.RegisterServlet;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

@WebServlet("/RegisterCodeServlet")
public class RegisterCodeServlet  extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        req.setCharacterEncoding("utf-8");
        String type = req.getParameter("appType");
        try {
            Class<?> c = this.getClass();
            Method me = c.getMethod("Service_" + type, HttpServletRequest.class, HttpServletResponse.class);
            me.invoke(this, req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //获得一次验证码行为
    public void Service_GetCode(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException
    {
        resp.setContentType("application/json;charset=utf-8");
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(50,25,4,5);
        String s = lineCaptcha.getCode();
        req.getSession().setAttribute("Register_getCode", s);
        lineCaptcha.write(resp.getOutputStream());

    }

}
